<!DOCTYPE html>
<!-- saved from url=(0047)https://www.cnblogs.com/9dragon/p/10901448.html -->
<html lang="zh-cn"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="referrer" content="origin">
    <meta property="og:description" content="一、序列化的含义、意义及使用场景二、序列化实现的方式1、Serializable1.1 普通序列化1.2 成员是引用的序列化1.3 同一对象序列化多次的机制1.4 java序列化算法潜在的问题1.5 ">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>java序列化，看这篇就够了 - 9龙 - 博客园</title>
    
    <link rel="stylesheet" href="./java序列化，看这篇就够了 - 9龙 - 博客园_files/blog-common.min.css">
    <link id="MainCss" rel="stylesheet" href="./java序列化，看这篇就够了 - 9龙 - 博客园_files/bundle-codinglife.min.css">
    <link type="text/css" rel="stylesheet" href="./java序列化，看这篇就够了 - 9龙 - 博客园_files/custom.css">
    <link id="mobile-style" media="only screen and (max-width: 767px)" type="text/css" rel="stylesheet" href="./java序列化，看这篇就够了 - 9龙 - 博客园_files/bundle-codinglife-mobile.min.css">
    
    <link type="application/rss+xml" rel="alternate" href="https://www.cnblogs.com/9dragon/rss">
    <link type="application/rsd+xml" rel="EditURI" href="https://www.cnblogs.com/9dragon/rsd.xml">
    <link type="application/wlwmanifest+xml" rel="wlwmanifest" href="https://www.cnblogs.com/9dragon/wlwmanifest.xml">
    <script src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/amp4ads-host-v0.js.下载"></script><script async="" src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/analytics.js.下载"></script><script src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/jquery-2.2.0.min.js.下载"></script>
    <script src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/blog-common.min.js.下载"></script>
    <script>
        var currentBlogId = 507859;
        var currentBlogApp = '9dragon';
        var cb_enable_mathjax = false;
        var isLogined = false;
        var skinName = 'CodingLife';
    </script>
    
    
    
<link rel="preload" href="./java序列化，看这篇就够了 - 9龙 - 博客园_files/f.txt" as="script"><script type="text/javascript" src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/f.txt"></script><script src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/pubads_impl_2020051103.js.下载" async=""></script><link rel="prefetch" href="https://da477c0e2c53471f9969cd3d41ebc5dd.safeframe.googlesyndication.com/safeframe/1-0-37/html/container.html"><link rel="prefetch" href="https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html"></head>
<body>
    <a name="top"></a>
    
    
<!--done-->
<div id="home">
<div id="header">
	<div id="blogTitle">
        <a id="lnkBlogLogo" href="https://www.cnblogs.com/9dragon/"><img id="blogLogo" src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/logo.gif" alt="返回主页"></a>		
		
<!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle HeaderMainTitle" href="https://www.cnblogs.com/9dragon/">9龙</a>
</h1>
<h2>

</h2>




		
	</div><!--end: blogTitle 博客的标题和副标题 -->
	<div id="navigator">
		
<ul id="navList">
<li><a id="blog_nav_sitehome" class="menu" href="https://www.cnblogs.com/">
博客园</a>
</li>
<li>
<a id="blog_nav_myhome" class="menu" href="https://www.cnblogs.com/9dragon/">
首页</a>
</li>
<li>

<a id="blog_nav_newpost" class="menu" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">
新随笔</a>
</li>
<li>
<a id="blog_nav_contact" class="menu" href="https://msg.cnblogs.com/send/9%E9%BE%99">
联系</a></li>
<li>
<a id="blog_nav_rss" class="menu" href="javascript:void(0)" data-rss="https://www.cnblogs.com/9dragon/rss/">
订阅</a>
<!--<partial name="./Shared/_XmlLink.cshtml" model="Model" /></li>--></li>
<li>
<a id="blog_nav_admin" class="menu" href="https://i.cnblogs.com/">
管理</a>
</li>
</ul>


		<div class="blogStats">
			
			<span id="stats_post_count">随笔 - 
20&nbsp; </span>
<span id="stats_article_count">文章 - 
0&nbsp; </span>
<span id="stats-comment_count">评论 - 
20</span>

			
		</div><!--end: blogStats -->
	</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 -->

<div id="main">
	<div id="mainContent">
	<div class="forFlow">
		<div id="post_detail">
    <!--done-->
    <div id="topics">
        <div class="post">
            <h1 class="postTitle">
                
<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/9dragon/p/10901448.html">java序列化，看这篇就够了</a>

            </h1>
            <div class="clear"></div>
            <div class="postBody">
                
<div id="cnblogs_post_body" class="blogpost-body cnblogs-markdown">
    <div class="output_wrapper" id="output_wrapper_id" style="font-size: 15px; color: rgb(62, 62, 62); line-height: 1.8; word-spacing: 2px; letter-spacing: 2px; font-family: &#39;Helvetica Neue&#39;, Helvetica, &#39;Hiragino Sans GB&#39;, &#39;Microsoft YaHei&#39;, Arial, sans-serif; background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center center;"><p class="toc" id="tocid_0" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px; margin-left: 25px;"><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">一、序列化的含义、意义及使用场景</a></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h-1" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">二、序列化实现的方式</a></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h1serializable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">1、Serializable</a></span></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h11" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">1.1 普通序列化</a></span></span></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h12" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">1.2 成员是引用的序列化</a></span></span></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h13" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">1.3 同一对象序列化多次的机制</a></span></span></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h14java" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">1.4 java序列化算法潜在的问题</a></span></span></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h15" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">1.5 可选的自定义序列化</a></span></span></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h2externalizable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">2、Externalizable：强制自定义序列化</a></span></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h3" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">3、两种序列化对比</a></span></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#hserialversionuid" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">三、序列化版本号serialVersionUID</a></span></span><span class="toc_item" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: block;"><span class="toc_left" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-left: 25px;"><a href="https://www.cnblogs.com/9dragon/p/10901448.html#h-2" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; color: rgb(30, 107, 184); overflow-wrap: break-word;">四、总结</a></span></span></p>
<h3 id="h" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">一、序列化的含义、意义及使用场景</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;"> </span></h3>
<ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">序列化：将对象写入到IO流中</strong></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">反序列化：从IO流中恢复对象</strong></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">意义：序列化机制允许将实现序列化的Java对象转换位字节序列，这些字节序列可以保存在磁盘上，或通过网络传输，以达到以后恢复成原来的对象。序列化机制使得对象可以脱离程序的运行而独立存在。</strong></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">使用场景：所有可在网络上传输的对象都必须是可序列化的，</strong>比如RMI（remote method invoke,即远程方法调用），传入的参数或返回的对象都是可序列化的，否则会出错；<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">所有需要保存到磁盘的java对象都必须是可序列化的。通常建议：程序创建的每个JavaBean类都实现Serializeable接口。</strong></li>
</ul>
<h3 id="h-1" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">二、序列化实现的方式</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;"> </span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">如果需要将某个对象保存到磁盘上或者通过网络传输，那么这个类应该实现<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">Serializable</strong>接口或者<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">Externalizable</strong>接口之一。</p>
<h4 id="h1serializable" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">1、Serializable</span></h4>
<h5 id="h11" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">1.1 普通序列化</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">Serializable接口是一个标记接口，不用实现任何方法。一旦实现了此接口，该类的对象就是可序列化的。</p>
<ol style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: decimal;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">序列化步骤：</strong></li>
</ol>
<ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">步骤一：创建一个ObjectOutputStream输出流；</strong></p></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">步骤二：调用ObjectOutputStream对象的writeObject输出可序列化对象。</strong></p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Person</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">implements</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Serializable</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;String&nbsp;name;<br>&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">int</span></span>&nbsp;age;<br>&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//我不提供无参构造器</span></span><br><span class="hljs-comment">&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">Person</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String&nbsp;name,&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment">&nbsp;age)</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.name&nbsp;=&nbsp;name;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.age&nbsp;=&nbsp;age;</span><br><span class="hljs-comment">&nbsp;&nbsp;}</span><br><br><span class="hljs-comment">&nbsp;&nbsp;</span><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">@Override</span></span><br><span class="hljs-comment">&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;String&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">toString</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">()</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">return</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"Person{"</span></span><span class="hljs-comment">&nbsp;+</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"name='"</span></span><span class="hljs-comment">&nbsp;+&nbsp;name&nbsp;+&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">'\''</span></span><span class="hljs-comment">&nbsp;+</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">",&nbsp;age="</span></span><span class="hljs-comment">&nbsp;+&nbsp;age&nbsp;+</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">'}'</span></span><span class="hljs-comment">;</span><br><span class="hljs-comment">&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">class</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">WriteObject</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">static</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">main</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String[]&nbsp;args)</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">try</span></span><span class="hljs-comment">&nbsp;(</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//创建一个ObjectOutputStream输出流</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectOutputStream&nbsp;oos&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectOutputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileOutputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"object.txt"</span></span><span class="hljs-comment">)))&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//将对象序列化到文件s</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;person&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;Person(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"9龙"</span></span><span class="hljs-comment">,&nbsp;</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">23</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(person);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">catch</span></span><span class="hljs-comment">&nbsp;(Exception&nbsp;e)&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br></code></pre></li>
</ul>
<ol style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: decimal;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">反序列化步骤：</strong></li>
</ol>
<ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">步骤一：创建一个ObjectInputStream输入流；</strong></p></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">步骤二：调用ObjectInputStream对象的readObject()得到序列化的对象。</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">我们将上面序列化到person.txt的person对象反序列化回来</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Person</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">implements</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Serializable</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;String&nbsp;name;<br>&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">int</span></span>&nbsp;age;<br>&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//我不提供无参构造器</span></span><br><span class="hljs-comment">&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">Person</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String&nbsp;name,&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment">&nbsp;age)</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"反序列化，你调用我了吗？"</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.name&nbsp;=&nbsp;name;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.age&nbsp;=&nbsp;age;</span><br><span class="hljs-comment">&nbsp;&nbsp;}</span><br><br><span class="hljs-comment">&nbsp;&nbsp;</span><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">@Override</span></span><br><span class="hljs-comment">&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;String&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">toString</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">()</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">return</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"Person{"</span></span><span class="hljs-comment">&nbsp;+</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"name='"</span></span><span class="hljs-comment">&nbsp;+&nbsp;name&nbsp;+&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">'\''</span></span><span class="hljs-comment">&nbsp;+</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">",&nbsp;age="</span></span><span class="hljs-comment">&nbsp;+&nbsp;age&nbsp;+</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">'}'</span></span><span class="hljs-comment">;</span><br><span class="hljs-comment">&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">class</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">ReadObject</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">static</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">main</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String[]&nbsp;args)</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">try</span></span><span class="hljs-comment">&nbsp;(</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//创建一个ObjectInputStream输入流</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectInputStream&nbsp;ois&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectInputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileInputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"person.txt"</span></span><span class="hljs-comment">)))&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;brady&nbsp;=&nbsp;(Person)&nbsp;ois.readObject();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(brady);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">catch</span></span><span class="hljs-comment">&nbsp;(Exception&nbsp;e)&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//输出结果</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//Person{name='9龙',&nbsp;age=23}</span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">waht????    输出告诉我们，反序列化并不会调用构造方法。反序列的对象是由JVM自己生成的对象，不通过构造方法生成。</strong></p></li>
</ul>
<h5 id="h12" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">1.2 成员是引用的序列化</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">如果一个可序列化的类的成员不是基本类型，也不是String类型，那这个引用类型也必须是可序列化的；否则，会导致此类不能序列化。</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">看例子，我们新增一个Teacher类。将Person去掉实现Serializable接口代码。</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Person</span></span></span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//省略相关属性与方法</span></span><br><span class="hljs-comment">}</span><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">class</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">Teacher</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">implements</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">Serializable</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;String&nbsp;name;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;Person&nbsp;person;</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">Teacher</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String&nbsp;name,&nbsp;Person&nbsp;person)</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.name&nbsp;=&nbsp;name;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.person&nbsp;=&nbsp;person;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">static</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">main</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String[]&nbsp;args)</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;Exception&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">try</span></span><span class="hljs-comment">&nbsp;(ObjectOutputStream&nbsp;oos&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectOutputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileOutputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"teacher.txt"</span></span><span class="hljs-comment">)))&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;person&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;Person(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"路飞"</span></span><span class="hljs-comment">,&nbsp;</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">20</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Teacher&nbsp;teacher&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;Teacher(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"雷利"</span></span><span class="hljs-comment">,&nbsp;person);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(teacher);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br></code></pre>
<figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/1603499-20190521180304399-894547036.jpg" alt="" title="" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption></figure>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">我们看到程序直接报错，因为Person类的对象是不可序列化的，这导致了Teacher的对象不可序列化</p>
<h5 id="h13" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">1.3 同一对象序列化多次的机制</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">同一对象序列化多次，会将这个对象序列化多次吗？</strong>答案是<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">否定</strong>的。</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="Java language-Java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">WriteTeacher</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">static</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-title">main</span></span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-params">(String[]&nbsp;args)</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">throws</span></span></span><span class="hljs-function">&nbsp;Exception&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">try</span></span>&nbsp;(ObjectOutputStream&nbsp;oos&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;ObjectOutputStream(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;FileOutputStream(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-string">"teacher.txt"</span></span>)))&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;person&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;Person(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-string">"路飞"</span></span>,&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-number">20</span></span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Teacher&nbsp;t1&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;Teacher(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-string">"雷利"</span></span>,&nbsp;person);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Teacher&nbsp;t2&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;Teacher(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-string">"红发香克斯"</span></span>,&nbsp;person);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//依次将4个对象写入输入流</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(t1);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(t2);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(person);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(t2);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">依次将t1、t2、person、t2对象序列化到文件teacher.txt文件中。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">注意：反序列化的顺序与序列化时的顺序一致</strong>。</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">ReadTeacher</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">static</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-title">main</span></span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-params">(String[]&nbsp;args)</span></span></span><span class="hljs-function">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">try</span></span>&nbsp;(ObjectInputStream&nbsp;ois&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;ObjectInputStream(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;FileInputStream(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-string">"teacher.txt"</span></span>)))&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Teacher&nbsp;t1&nbsp;=&nbsp;(Teacher)&nbsp;ois.readObject();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Teacher&nbsp;t2&nbsp;=&nbsp;(Teacher)&nbsp;ois.readObject();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;p&nbsp;=&nbsp;(Person)&nbsp;ois.readObject();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Teacher&nbsp;t3&nbsp;=&nbsp;(Teacher)&nbsp;ois.readObject();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(t1&nbsp;==&nbsp;t2);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(t1.getPerson()&nbsp;==&nbsp;p);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(t2.getPerson()&nbsp;==&nbsp;p);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(t2&nbsp;==&nbsp;t3);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(t1.getPerson()&nbsp;==&nbsp;t2.getPerson());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">catch</span></span>&nbsp;(Exception&nbsp;e)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//输出结果</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//false</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//true</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//true</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//true</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//true</span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">从输出结果可以看出，<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">Java序列化同一对象，并不会将此对象序列化多次得到多个对象。</strong></p>
<ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">Java序列化算法</strong></li>
</ul>
<ol style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: decimal;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">所有保存到磁盘的对象都有一个序列化编码号</strong></p></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">当程序试图序列化一个对象时，会先检查此对象是否已经序列化过，只有此对象从未（在此虚拟机）被序列化过，才会将此对象序列化为字节序列输出。</strong></p></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">如果此对象已经序列化过，则直接输出编号即可。</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">图示上述序列化过程。</p></li>
</ol>
<figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/1603499-20190521180352659-740977206.jpg" alt="" title="" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption></figure>
<h5 id="h14java" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">1.4 java序列化算法潜在的问题</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">由于java序利化算法不会重复序列化同一个对象，只会记录已序列化对象的编号。<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">如果序列化一个可变对象（对象内的内容可更改）后，更改了对象内容，再次序列化，并不会再次将此对象转换为字节序列，而只是保存序列化编号。</strong></p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">WriteObject</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">static</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-title">main</span></span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-params">(String[]&nbsp;args)</span></span></span><span class="hljs-function">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">try</span></span>&nbsp;(ObjectOutputStream&nbsp;oos&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;ObjectOutputStream(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;FileOutputStream(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-string">"person.txt"</span></span>));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectInputStream&nbsp;ios&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;ObjectInputStream(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">new</span></span>&nbsp;FileInputStream(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-string">"person.txt"</span></span>)))&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//第一次序列化person</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;person&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;Person(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"9龙"</span></span><span class="hljs-comment">,&nbsp;</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">23</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(person);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(person);</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//修改name</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;person.setName(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"海贼王"</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(person);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//第二次序列化person</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(person);</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//依次反序列化出p1、p2</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;p1&nbsp;=&nbsp;(Person)&nbsp;ios.readObject();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;p2&nbsp;=&nbsp;(Person)&nbsp;ios.readObject();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(p1&nbsp;==&nbsp;p2);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(p1.getName().equals(p2.getName()));</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">catch</span></span><span class="hljs-comment">&nbsp;(Exception&nbsp;e)&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//输出结果</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//Person{name='9龙',&nbsp;age=23}</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//Person{name='海贼王',&nbsp;age=23}</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//true</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//true</span></span><br></code></pre>
<h5 id="h15" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">1.5 可选的自定义序列化</span></h5>
<ol style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: decimal;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">有些时候，我们有这样的需求，某些属性不需要序列化。<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">使用transient关键字选择不需要序列化的字段。</strong></p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Person</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">implements</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Serializable</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//不需要序列化名字与年龄</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">transient</span></span><span class="hljs-comment">&nbsp;String&nbsp;name;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">transient</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment">&nbsp;age;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment">&nbsp;height;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">transient</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">boolean</span></span><span class="hljs-comment">&nbsp;singlehood;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">Person</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String&nbsp;name,&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment">&nbsp;age)</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.name&nbsp;=&nbsp;name;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.age&nbsp;=&nbsp;age;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//省略get,set方法</span></span><br><span class="hljs-comment">}</span><br><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">class</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">TransientTest</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">static</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">main</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String[]&nbsp;args)</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;Exception&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">try</span></span><span class="hljs-comment">&nbsp;(ObjectOutputStream&nbsp;oos&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectOutputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileOutputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"person.txt"</span></span><span class="hljs-comment">));</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectInputStream&nbsp;ios&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectInputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileInputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"person.txt"</span></span><span class="hljs-comment">)))&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;person&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;Person(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"9龙"</span></span><span class="hljs-comment">,&nbsp;</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">23</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;person.setHeight(</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">185</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(person);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(person);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;p1&nbsp;=&nbsp;(Person)ios.readObject();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(p1);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//输出结果</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//Person{name='9龙',&nbsp;age=23',&nbsp;singlehood=true',&nbsp;height=185cm}</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//Person{name='null',&nbsp;age=0',&nbsp;singlehood=false',&nbsp;height=185cm}</span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">从输出我们看到，<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">使用transient修饰的属性，java序列化时，会忽略掉此字段，所以反序列化出的对象，被transient修饰的属性是默认值。对于引用类型，值是null；基本类型，值是0；boolean类型，值是false。</strong></p></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">使用transient虽然简单，但将此属性完全隔离在了序列化之外。java提供了<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">可选的自定义序列化。</strong>可以进行控制序列化的方式，或者对序列化数据进行编码加密等。</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">private</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-title">writeObject</span></span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-params">(java.io.ObjectOutputStream&nbsp;out)</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">throws</span></span></span><span class="hljs-function">&nbsp;IOException；</span><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">private</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-title">readObject</span></span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-params">(java.io.ObjectIutputStream&nbsp;in)</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">throws</span></span></span><span class="hljs-function">&nbsp;IOException,ClassNotFoundException</span></span>;<br><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">private</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-title">readObjectNoData</span></span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-params">()</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">throws</span></span></span><span class="hljs-function">&nbsp;ObjectStreamException</span></span>;<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">通过重写writeObject与readObject方法，可以自己选择哪些属性需要序列化， 哪些属性不需要。如果writeObject使用某种规则序列化，则相应的readObject需要相反的规则反序列化，以便能正确反序列化出对象。这里展示对名字进行反转加密。</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Person</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">implements</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Serializable</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;String&nbsp;name;<br>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">int</span></span>&nbsp;age;<br>&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//省略构造方法，get及set方法</span></span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">writeObject</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(ObjectOutputStream&nbsp;out)</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;IOException&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//将名字反转写入二进制流</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.writeObject(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;StringBuffer(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.name).reverse());</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.writeInt(age);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;}</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">readObject</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(ObjectInputStream&nbsp;ins)</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;IOException,ClassNotFoundException</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//将读出的字符串反转恢复回来</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.name&nbsp;=&nbsp;((StringBuffer)ins.readObject()).reverse().toString();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.age&nbsp;=&nbsp;ins.readInt();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">当序列化流不完整时，readObjectNoData()方法可以用来正确地初始化反序列化的对象。例如，使用不同类接收反序列化对象，或者序列化流被篡改时，系统都会调用readObjectNoData()方法来初始化反序列化的对象。</p></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">更彻底的自定义序列化</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;<br>  ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;</p>
<ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">writeReplace：在序列化时，会先调用此方法，再调用writeObject方法。此方法可将任意对象代替目标序列化对象</strong></p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Person</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">implements</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Serializable</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;String&nbsp;name;<br>&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">int</span></span>&nbsp;age;<br>&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//省略构造方法，get及set方法</span></span><br><br><span class="hljs-comment">&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;Object&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">writeReplace</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">()</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;ObjectStreamException&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ArrayList&lt;Object&gt;&nbsp;list&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ArrayList&lt;&gt;(</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">2</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.name);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.age);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">return</span></span><span class="hljs-comment">&nbsp;list;</span><br><span class="hljs-comment">&nbsp;&nbsp;}</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">static</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">main</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String[]&nbsp;args)</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;Exception&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">try</span></span><span class="hljs-comment">&nbsp;(ObjectOutputStream&nbsp;oos&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectOutputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileOutputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"person.txt"</span></span><span class="hljs-comment">));</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectInputStream&nbsp;ios&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectInputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileInputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"person.txt"</span></span><span class="hljs-comment">)))&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;person&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;Person(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"9龙"</span></span><span class="hljs-comment">,&nbsp;</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">23</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(person);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ArrayList&nbsp;list&nbsp;=&nbsp;(ArrayList)ios.readObject();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(list);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//输出结果</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//[9龙,&nbsp;23]</span></span><br></code></pre></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">readResolve：反序列化时替换反序列化出的对象，反序列化出来的对象被立即丢弃。此方法在readeObject后调用。</strong></p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Person</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">implements</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Serializable</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;String&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">int</span></span>&nbsp;age;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//省略构造方法，get及set方法</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;Object&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">readResolve</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">()</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;ObjectStreamException</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">return</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"brady"</span></span><span class="hljs-comment">,&nbsp;</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">23</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">static</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">main</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String[]&nbsp;args)</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;Exception&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">try</span></span><span class="hljs-comment">&nbsp;(ObjectOutputStream&nbsp;oos&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectOutputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileOutputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"person.txt"</span></span><span class="hljs-comment">));</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectInputStream&nbsp;ios&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectInputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileInputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"person.txt"</span></span><span class="hljs-comment">)))&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person&nbsp;person&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;Person(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"9龙"</span></span><span class="hljs-comment">,&nbsp;</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">23</span></span><span class="hljs-comment">);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(person);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HashMap&nbsp;map&nbsp;=&nbsp;(HashMap)ios.readObject();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(map);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//输出结果</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//{brady=23}</span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">readResolve常用来反序列单例类，保证单例类的唯一性。</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">注意：readResolve与writeReplace的访问修饰符可以是private、protected、public，如果父类重写了这两个方法，子类都需要根据自身需求重写，这显然不是一个好的设计。通常建议对于final修饰的类重写readResolve方法没有问题；否则，重写readResolve使用private修饰。</strong></p></li></ul></li>
</ol>
<h4 id="h2externalizable" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">2、Externalizable：强制自定义序列化</span></h4>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">通过实现Externalizable接口，必须实现writeExternal、readExternal方法。</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">interface</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Externalizable</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">extends</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">java</span></span></span><span class="hljs-class">.</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">io</span></span></span><span class="hljs-class">.</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Serializable</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-title">writeExternal</span></span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-params">(ObjectOutput&nbsp;out)</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">throws</span></span></span><span class="hljs-function">&nbsp;IOException</span></span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-title">readExternal</span></span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-params">(ObjectInput&nbsp;in)</span></span></span><span class="hljs-function">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-function"><span class="hljs-keyword">throws</span></span></span><span class="hljs-function">&nbsp;IOException,&nbsp;ClassNotFoundException</span></span>;<br>}<br></code></pre>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">ExPerson</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">implements</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Externalizable</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;String&nbsp;name;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">private</span></span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">int</span></span>&nbsp;age;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//注意，必须加上pulic&nbsp;无参构造器</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">ExPerson</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">()</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">ExPerson</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String&nbsp;name,&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment">&nbsp;age)</span></span><span class="hljs-comment">&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.name&nbsp;=&nbsp;name;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.age&nbsp;=&nbsp;age;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">@Override</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">writeExternal</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(ObjectOutput&nbsp;out)</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;IOException&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//将name反转后写入二进制流</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer&nbsp;reverse&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;StringBuffer(name).reverse();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(reverse.toString());</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.writeObject(reverse);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.writeInt(age);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">@Override</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">readExternal</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(ObjectInput&nbsp;in)</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;IOException,&nbsp;ClassNotFoundException&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//将读取的字符串反转后赋值给name实例变量</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.name&nbsp;=&nbsp;((StringBuffer)&nbsp;in.readObject()).reverse().toString();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(name);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">this</span></span><span class="hljs-comment">.age&nbsp;=&nbsp;in.readInt();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-function" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">public</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">static</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">void</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">main</span></span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">(String[]&nbsp;args)</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">throws</span></span><span class="hljs-comment">&nbsp;IOException,&nbsp;ClassNotFoundException&nbsp;</span></span><span class="hljs-comment">{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">try</span></span><span class="hljs-comment">&nbsp;(ObjectOutputStream&nbsp;oos&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectOutputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileOutputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"ExPerson.txt"</span></span><span class="hljs-comment">));</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectInputStream&nbsp;ois&nbsp;=&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ObjectInputStream(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;FileInputStream(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"ExPerson.txt"</span></span><span class="hljs-comment">)))&nbsp;{</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">new</span></span><span class="hljs-comment">&nbsp;ExPerson(</span><span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(42, 146, 146); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">"brady"</span></span><span class="hljs-comment">,&nbsp;</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">23</span></span><span class="hljs-comment">));</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ExPerson&nbsp;ep&nbsp;=&nbsp;(ExPerson)&nbsp;ois.readObject();</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(ep);</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br><span class="hljs-comment">}</span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//输出结果</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//ydarb</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//brady</span></span><br><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//ExPerson{name='brady',&nbsp;age=23}</span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">注意：Externalizable接口不同于Serializable接口，实现此接口必须实现接口中的两个方法实现自定义序列化，这是强制性的；特别之处是必须提供pulic的无参构造器，因为在反序列化的时候需要反射创建对象。</strong></p>
<h4 id="h3" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; font-size: 1.2em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">3、两种序列化对比</span></h4>
<table style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; display: table; width: 100%; text-align: left;">
<thead style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">
<tr style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: rgb(204, 204, 204); background-color: white;">
<th style="color: inherit; line-height: inherit; margin: 0px; font-size: 1em; border: 1px solid rgb(204, 204, 204); padding: 0.5em 1em; text-align: left; font-weight: bold; background-color: rgb(240, 240, 240);">实现Serializable接口</th>
<th style="color: inherit; line-height: inherit; margin: 0px; font-size: 1em; border: 1px solid rgb(204, 204, 204); padding: 0.5em 1em; text-align: left; font-weight: bold; background-color: rgb(240, 240, 240);">实现Externalizable接口</th>
</tr>
</thead>
<tbody style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; border: 0px;">
<tr style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: rgb(204, 204, 204); background-color: white;">
<td style="color: inherit; line-height: inherit; margin: 0px; font-size: 1em; border: 1px solid rgb(204, 204, 204); padding: 0.5em 1em; text-align: left;">系统自动存储必要的信息</td>
<td style="color: inherit; line-height: inherit; margin: 0px; font-size: 1em; border: 1px solid rgb(204, 204, 204); padding: 0.5em 1em; text-align: left;">程序员决定存储哪些信息</td>
</tr>
<tr style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: rgb(204, 204, 204); background-color: rgb(248, 248, 248);">
<td style="color: inherit; line-height: inherit; margin: 0px; font-size: 1em; border: 1px solid rgb(204, 204, 204); padding: 0.5em 1em; text-align: left;">Java内建支持，易于实现，只需要实现该接口即可，无需任何代码支持</td>
<td style="color: inherit; line-height: inherit; margin: 0px; font-size: 1em; border: 1px solid rgb(204, 204, 204); padding: 0.5em 1em; text-align: left;">必须实现接口内的两个方法</td>
</tr>
<tr style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: rgb(204, 204, 204); background-color: white;">
<td style="color: inherit; line-height: inherit; margin: 0px; font-size: 1em; border: 1px solid rgb(204, 204, 204); padding: 0.5em 1em; text-align: left;">性能略差</td>
<td style="color: inherit; line-height: inherit; margin: 0px; font-size: 1em; border: 1px solid rgb(204, 204, 204); padding: 0.5em 1em; text-align: left;">性能略好</td>
</tr>
</tbody>
</table>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">虽然Externalizable接口带来了一定的性能提升，但变成复杂度也提高了，所以一般通过实现Serializable接口进行序列化。</strong></p>
<h3 id="hserialversionuid" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">三、序列化版本号serialVersionUID</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;"> </span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">我们知道，<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">反序列化必须拥有class文件，但随着项目的升级，class文件也会升级，序列化怎么保证升级前后的兼容性呢？</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">java序列化提供了一个private static final long serialVersionUID 的序列化版本号，只有版本号相同，即使更改了序列化属性，对象也可以正确被反序列化回来。</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="java language-java hljs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; background: rgb(25, 23, 28); color: rgb(139, 135, 146); padding: 0.5em; display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword">public</span></span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Person</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-keyword">implements</span></span></span><span class="hljs-class">&nbsp;</span><span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(87, 109, 219); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-class"><span class="hljs-title">Serializable</span></span></span><span class="hljs-class">&nbsp;</span></span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//序列化版本号</span></span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">static</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">final</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">long</span></span><span class="hljs-comment">&nbsp;serialVersionUID&nbsp;=&nbsp;</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(170, 87, 60); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">1111013L</span></span><span class="hljs-comment">;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;String&nbsp;name;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">private</span></span><span class="hljs-comment">&nbsp;</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(149, 90, 231); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment">&nbsp;age;</span><br><span class="hljs-comment">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(126, 120, 135); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-comment">//省略构造方法及get,set</span></span><br><span class="hljs-comment">}</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">如果反序列化使用的<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">class的版本号</strong>与序列化时使用的<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">不一致</strong>，反序列化会<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">报InvalidClassException异常。</strong></p>
<figure style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><img src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/1603499-20190521180432865-1645890598.jpg" alt="" title="" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;"></figcaption></figure>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;"><strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">序列化版本号可自由指定，如果不指定，JVM会根据类信息自己计算一个版本号，这样随着class的升级，就无法正确反序列化；不指定版本号另一个明显隐患是，不利于jvm间的移植，可能class文件没有更改，但不同jvm可能计算的规则不一样，这样也会导致无法反序列化。</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.7em 0px;">什么情况下需要修改serialVersionUID呢？分三种情况。</p>
<ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: disc;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">如果只是修改了方法，反序列化不容影响，则无需修改版本号；</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">如果只是修改了静态变量，瞬态变量（transient修饰的变量），反序列化不受影响，无需修改版本号；</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;">如果修改了非瞬态变量，则可能导致反序列化失败。<strong style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold; color: rgb(233, 105, 0);">如果新类中实例变量的类型与序列化时类的类型不一致，则会反序列化失败，这时候需要更改serialVersionUID。</strong>如果只是新增了实例变量，则反序列化回来新增的是默认值；如果减少了实例变量，反序列化时会忽略掉减少的实例变量。</li>
</ul>
<h3 id="h-2" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.6em 0px; font-weight: bold; border-bottom: 2px solid rgb(239, 112, 96); font-size: 1.3em;"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(239, 112, 96); color: rgb(255, 255, 255); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;">四、总结</span><span style="display: inline-block; vertical-align: bottom; border-bottom: 36px solid rgb(239, 235, 233); border-right: 20px solid transparent;"> </span></h3>
<ol style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; padding-left: 32px; list-style-type: decimal;">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">所有需要网络传输的对象都需要实现序列化接口，通过建议所有的javaBean都实现Serializable接口。</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">对象的类名、实例变量（包括基本类型，数组，对其他对象的引用）都会被序列化；方法、类变量、transient实例变量都不会被序列化。</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">如果想让某个变量不被序列化，使用transient修饰。</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">序列化对象的引用类型成员变量，也必须是可序列化的，否则，会报错。</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">反序列化时必须有序列化对象的class文件。</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">当通过文件、网络来读取序列化后的对象时，必须按照实际写入的顺序读取。</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">单例类序列化，需要重写readResolve()方法；否则会破坏单例原则。</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">同一对象序列化多次，只有第一次序列化为二进制流，以后都只是保存序列化编号，不会重复序列化。</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; margin-bottom: 0.5em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">建议所有可序列化的类加上serialVersionUID 版本号，方便项目升级。</span></li>
</ol></div>
</div>
<div id="MySignature" style="display: block;"><div>作者：<a href="https://www.cnblogs.com/9dragon/" target="_blank">9龙</a></div>
<div>出处：<a href="https://www.cnblogs.com/9dragon/" target="_blank">https://www.cnblogs.com/9dragon/</a></div>
<div>本文版权归作者和博客园共有，欢迎转载，但必须给出原文链接，并保留此段声明。</div></div>
<div class="clear"></div>
<div id="blog_post_info_block"><div id="BlogPostCategory">
    分类: 
            <a href="https://www.cnblogs.com/9dragon/category/1439776.html" target="_blank">Java基础</a></div>
<div id="EntryTag">
    标签: 
            <a href="https://www.cnblogs.com/9dragon/tag/java%E5%9F%BA%E7%A1%80/">java基础</a>,             <a href="https://www.cnblogs.com/9dragon/tag/%E5%BA%8F%E5%88%97%E5%8C%96/">序列化</a></div>

    <div id="blog_post_info">
<div id="green_channel">
        <a href="javascript:void(0);" id="green_channel_digg" onclick="DiggIt(10901448,cb_blogId,1);green_channel_success(this,&#39;谢谢推荐！&#39;);">好文要顶</a>
        <a id="green_channel_follow" onclick="follow(&#39;8a2ececb-93f4-4dbd-b781-08d692b5b804&#39;);" href="javascript:void(0);">关注我</a>
    <a id="green_channel_favorite" onclick="AddToWz(cb_entryId);return false;" href="javascript:void(0);">收藏该文</a>
    <a id="green_channel_weibo" href="javascript:void(0);" title="分享至新浪微博" onclick="ShareToTsina()"><img src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/icon_weibo_24.png" alt=""></a>
    <a id="green_channel_wechat" href="javascript:void(0);" title="分享至微信" onclick="shareOnWechat()"><img src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/wechat.png" alt=""></a>
</div>
<div id="author_profile">
    <div id="author_profile_info" class="author_profile_info">
            <a href="https://home.cnblogs.com/u/9dragon/" target="_blank"><img src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/20190506185023.png" class="author_avatar" alt=""></a>
        <div id="author_profile_detail" class="author_profile_info">
            <a href="https://home.cnblogs.com/u/9dragon/">9龙</a><br>
            <a href="https://home.cnblogs.com/u/9dragon/followees/">关注 - 4</a><br>
            <a href="https://home.cnblogs.com/u/9dragon/followers/">粉丝 - 13</a>
        </div>
    </div>
    <div class="clear"></div>
    <div id="author_profile_honor"></div>
    <div id="author_profile_follow">
                <a href="javascript:void(0);" onclick="follow(&#39;8a2ececb-93f4-4dbd-b781-08d692b5b804&#39;);return false;">+加关注</a>
    </div>
</div>
<div id="div_digg">
    <div class="diggit" onclick="votePost(10901448,&#39;Digg&#39;)">
        <span class="diggnum" id="digg_count">15</span>
    </div>
    <div class="buryit" onclick="votePost(10901448,&#39;Bury&#39;)">
        <span class="burynum" id="bury_count">0</span>
    </div>
    <div class="clear"></div>
    <div class="diggword" id="digg_tips">
    </div>
</div>

<script type="text/javascript">
    currentDiggType = 0;
</script></div>
    <div class="clear"></div>
    <div id="post_next_prev">

    <a href="https://www.cnblogs.com/9dragon/p/10839472.html" class="p_n_p_prefix">« </a> 上一篇：    <a href="https://www.cnblogs.com/9dragon/p/10839472.html" title="发布于 2019-05-09 16:51">告别复杂的流关闭</a>
    <br>
    <a href="https://www.cnblogs.com/9dragon/p/10913289.html" class="p_n_p_prefix">» </a> 下一篇：    <a href="https://www.cnblogs.com/9dragon/p/10913289.html" title="发布于 2019-05-23 17:29">感受lambda之美，推荐收藏，需要时查阅</a>

</div>
</div>
            </div>
            <div class="postDesc">posted @ 
<span id="post-date">2019-05-21 18:06</span>&nbsp;
<a href="https://www.cnblogs.com/9dragon/">9龙</a>&nbsp;
阅读(<span id="post_view_count">18632</span>)&nbsp;
评论(<span id="post_comment_count">5</span>)&nbsp;
<a href="https://i.cnblogs.com/EditPosts.aspx?postid=10901448" rel="nofollow">编辑</a>&nbsp;
<a href="javascript:void(0)" onclick="AddToWz(10901448);return false;">收藏</a></div>
        </div>
	    
	    
    </div><!--end: topics 文章、评论容器-->
</div>
<script src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/highlight.min.js.下载"></script>
<script>markdown_highlight();</script>
<script>
    var allowComments = true, cb_blogId = 507859, cb_blogApp = '9dragon', cb_blogUserGuid = '8a2ececb-93f4-4dbd-b781-08d692b5b804';
    var cb_entryId = 10901448, cb_entryCreatedDate = '2019-05-21 18:06', cb_postType = 1; 
    loadViewCount(cb_entryId);
    loadSideColumnAd();
</script><a name="!comments"></a>
<div id="blog-comments-placeholder">

<div id="comment_pager_top">
    
</div>

<br>
<div class="feedback_area_title">评论列表</div>
<div class="feedbackNoItems"><div class="feedbackNoItems"></div></div>	
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;

<span class="comment_actions">
    
    
    
    
</span>


				</div>
				
<a href="https://www.cnblogs.com/9dragon/p/10901448.html#4261442" class="layer">#1楼</a>
<a name="4261442" id="comment_anchor_4261442"></a>

 
<span class="comment_date">2019-05-22 09:03</span>

 

            <a id="a_comment_author_4261442" href="https://www.cnblogs.com/gzu-pbj/" target="_blank">我要一步一步往上爬吖</a>

			</div>
			<div class="feedbackCon">
				
<div id="comment_body_4261442" data-format-type="Ubb" class="blog_comment_body cnblogs-ubb">
    真不错，收藏了，谢谢！
</div>
        <div class="comment_vote">
            <span class="comment_error" style="color: red"></span>
            <a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4261442, &#39;Digg&#39;, this.parentElement, false);">
                支持(1)
            </a>
            <a href="javascript:void(0);" class="comment_burry" onclick="return voteComment(4261442, &#39;Bury&#39;, this.parentElement, false);">
                反对(0)
            </a>
        </div>
        <span id="comment_4261442_avatar" style="display:none">
            https://pic.cnblogs.com/face/1504318/20190311215541.png
        </span>

			</div>
		</div>
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;

<span class="comment_actions">
    
    
    
    
</span>


				</div>
				
<a href="https://www.cnblogs.com/9dragon/p/10901448.html#4261483" class="layer">#2楼</a>
<a name="4261483" id="comment_anchor_4261483"></a>
[<span class="louzhu">楼主</span>]
 
<span class="comment_date">2019-05-22 09:28</span>

 

            <a id="a_comment_author_4261483" href="https://www.cnblogs.com/9dragon/" target="_blank">9龙</a>

			</div>
			<div class="feedbackCon">
				
<div id="comment_body_4261483" data-format-type="Ubb" class="blog_comment_body cnblogs-ubb">
    <a href="https://www.cnblogs.com/9dragon/p/10901448.html#4261442" title="查看所回复的评论" onclick="commentManager.renderComments(0,50,4261442);">@</a>
我要一步一步往上爬吖<br>共勉
</div>
        <div class="comment_vote">
            <span class="comment_error" style="color: red"></span>
            <a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4261483, &#39;Digg&#39;, this.parentElement, false);">
                支持(0)
            </a>
            <a href="javascript:void(0);" class="comment_burry" onclick="return voteComment(4261483, &#39;Bury&#39;, this.parentElement, false);">
                反对(0)
            </a>
        </div>
        <span id="comment_4261483_avatar" style="display:none">
            https://pic.cnblogs.com/face/1603499/20190506185023.png
        </span>

			</div>
		</div>
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;

<span class="comment_actions">
    
    
    
    
</span>


				</div>
				
<a href="https://www.cnblogs.com/9dragon/p/10901448.html#4473289" class="layer">#3楼</a>
<a name="4473289" id="comment_anchor_4473289"></a>

 
<span class="comment_date">2019-12-31 09:55</span>

 

            <a id="a_comment_author_4473289" href="https://www.cnblogs.com/chrd5/" target="_blank">头很痛</a>

			</div>
			<div class="feedbackCon">
				
<div id="comment_body_4473289" data-format-type="Ubb" class="blog_comment_body cnblogs-ubb">
    写得很透彻
</div>
        <div class="comment_vote">
            <span class="comment_error" style="color: red"></span>
            <a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4473289, &#39;Digg&#39;, this.parentElement, false);">
                支持(1)
            </a>
            <a href="javascript:void(0);" class="comment_burry" onclick="return voteComment(4473289, &#39;Bury&#39;, this.parentElement, false);">
                反对(0)
            </a>
        </div>
        <span id="comment_4473289_avatar" style="display:none">
            https://pic.cnblogs.com/face/1872622/20191119233205.png
        </span>

			</div>
		</div>
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;

<span class="comment_actions">
    
    
    
    
</span>


				</div>
				
<a href="https://www.cnblogs.com/9dragon/p/10901448.html#4518437" class="layer">#4楼</a>
<a name="4518437" id="comment_anchor_4518437"></a>

 
<span class="comment_date">2020-03-11 18:42</span>

 

            <a id="a_comment_author_4518437" href="https://home.cnblogs.com/u/892643/" target="_blank">手机疯</a>

			</div>
			<div class="feedbackCon">
				
<div id="comment_body_4518437" data-format-type="Markdown" class="blog_comment_body cnblogs-markdown">
    <p>写得很清晰，特意登陆点个赞！</p>

</div>
        <div class="comment_vote">
            <span class="comment_error" style="color: red"></span>
            <a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4518437, &#39;Digg&#39;, this.parentElement, false);">
                支持(0)
            </a>
            <a href="javascript:void(0);" class="comment_burry" onclick="return voteComment(4518437, &#39;Bury&#39;, this.parentElement, false);">
                反对(0)
            </a>
        </div>
        

			</div>
		</div>
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;

<span class="comment_actions">
    
    
    
    
</span>


				</div>
				
<a href="https://www.cnblogs.com/9dragon/p/10901448.html#4524382" class="layer">#5楼</a>
<a name="4524382" id="comment_anchor_4524382"></a>

        <span id="comment-maxId" style="display:none">4524382</span>
        <span id="comment-maxDate" style="display:none">2020/3/17 下午3:23:27</span>
 
<span class="comment_date">2020-03-17 15:23</span>

 

            <a id="a_comment_author_4524382" href="https://www.cnblogs.com/basterdaidai/" target="_blank">流氓呆呆</a>

			</div>
			<div class="feedbackCon">
				
<div id="comment_body_4524382" data-format-type="Markdown" class="blog_comment_body cnblogs-markdown">
    <p>别的不说，这排版是真的舒服</p>

</div>
        <div class="comment_vote">
            <span class="comment_error" style="color: red"></span>
            <a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4524382, &#39;Digg&#39;, this.parentElement, false);">
                支持(0)
            </a>
            <a href="javascript:void(0);" class="comment_burry" onclick="return voteComment(4524382, &#39;Bury&#39;, this.parentElement, false);">
                反对(0)
            </a>
        </div>
        

			</div>
		</div>

<div id="comment_pager_bottom">
    
</div>


</div>
<script>
    var commentManager = new blogCommentManager();
    commentManager.renderComments(0);
</script>

<div id="comment_form" class="commentform">
    <a name="commentform"></a>
    <div id="divCommentShow"></div>
    <div id="comment_nav"><span id="span_refresh_tips"></span><a href="javascript:void(0);" onclick="return RefreshCommentList();" id="lnk_RefreshComments" runat="server" clientidmode="Static">刷新评论</a><a href="https://www.cnblogs.com/9dragon/p/10901448.html#" onclick="return RefreshPage();">刷新页面</a><a href="https://www.cnblogs.com/9dragon/p/10901448.html#top">返回顶部</a></div>
    <div id="comment_form_container" style="visibility: visible;"><div class="login_tips">
    注册用户登录后才能发表评论，请 
    <a rel="nofollow" href="javascript:void(0);" class="underline" onclick="return login(&#39;commentform&#39;);">登录</a>
     或 
    <a rel="nofollow" href="javascript:void(0);" class="underline" onclick="return register();">注册</a>，
    <a href="https://www.cnblogs.com/">访问</a> 网站首页。
</div></div>
    <div class="ad_text_commentbox" id="ad_text_under_commentbox"></div>
    <div id="ad_t2"><a href="https://developer.huaweiuniversity.com/portal/courses/HuaweiX+CBGHWDCN030/about?source=bky" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T2-华为开发者学院文字链投放&#39;)">【推荐】AI大咖带您解锁深度学习算法，华为开发者学院直播间福利仅29元！</a><br><a href="http://www.ucancode.com/index.htm" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T2-ucancode&#39;)">【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库</a><br><a href="https://developer.aliyun.com/article/720790?utm_content=g_1000088945" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T2-阿里云开发者社区&#39;)">【推荐】《Flutter in action》开放下载！闲鱼Flutter企业级实践精选</a><br><a href="https://developer.aliyun.com/ask/257760?utm_content=g_1000088949" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T2-阿里云开发者社区&#39;)">【推荐】精品问答：大数据计算技术 1000 问</a><br></div>
    <div id="opt_under_post"></div>
    <script async="async" src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/gpt.js.下载"></script>
    <script>
        var googletag = googletag || {};
        googletag.cmd = googletag.cmd || [];
    </script>
    <script>
        googletag.cmd.push(function () {
            googletag.defineSlot("/1090369/C1", [300, 250], "div-gpt-ad-1546353474406-0").addService(googletag.pubads());
            googletag.defineSlot("/1090369/C2", [468, 60], "div-gpt-ad-1539008685004-0").addService(googletag.pubads());
            googletag.pubads().enableSingleRequest();
            googletag.enableServices();
        });
    </script>
    <div id="cnblogs_c1" class="c_ad_block">
        <div id="div-gpt-ad-1546353474406-0" style="height:250px; width:300px;" data-google-query-id="CNa92N_Rs-kCFQa_vQodF9gCQA"><div id="google_ads_iframe_/1090369/C1_0__container__" style="border: 0pt none;"><iframe id="google_ads_iframe_/1090369/C1_0" title="3rd party ad content" name="google_ads_iframe_/1090369/C1_0" width="300" height="250" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" style="border: 0px; vertical-align: bottom;" data-google-container-id="1" data-load-complete="true" src="./java序列化，看这篇就够了 - 9龙 - 博客园_files/saved_resource.html"></iframe></div></div>
    </div>
    <div id="under_post_news"><div class="recomm-block"><b>相关博文：</b><br>·  <a title="pythonf-string" href="https://www.cnblogs.com/c-x-a/p/9333826.html" target="_blank" onclick="clickRecomItmem(9333826)">pythonf-string</a><br>·  <a title="Vue学习看这篇就够" href="https://www.cnblogs.com/lvhw/p/8286544.html" target="_blank" onclick="clickRecomItmem(8286544)">Vue学习看这篇就够</a><br>·  <a title="Vue学习看这篇就够" href="https://www.cnblogs.com/bydzhangxiaowei/p/8723747.html" target="_blank" onclick="clickRecomItmem(8723747)">Vue学习看这篇就够</a><br>·  <a title="最实用的12条css技巧" href="https://www.cnblogs.com/JustinYoung/archive/2008/03/28/most-used-css-tricks.html" target="_blank" onclick="clickRecomItmem(1127840)">最实用的12条css技巧</a><br>·  <a title="Java分布式锁看这篇就够了" href="https://www.cnblogs.com/seesun2012/p/9214653.html" target="_blank" onclick="clickRecomItmem(9214653)">Java分布式锁看这篇就够了</a><br>»  <a target="_blank" href="https://recomm.cnblogs.com/blogpost/10901448">更多推荐...</a><div id="cnblogs_t5"><a href="https://developer.aliyun.com/article/740731?utm_content=g_1000104143https://developer.aliyun.com/article/740731?utm_content=g_1000104143" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T5-阿里云开发者社区&#39;)">2019 Flink Forward 大会最全视频来了！5大专题不容错过</a></div></div></div>
    <div id="cnblogs_c2" class="c_ad_block">
        <div id="div-gpt-ad-1539008685004-0" style="height:60px; width:468px;" data-google-query-id="CICq19_Rs-kCFQa_vQodF9gCQA">
            <script>
                if (new Date() >= new Date(2018, 9, 13)) {
                    googletag.cmd.push(function () { googletag.display("div-gpt-ad-1539008685004-0"); });
                }
            </script>
        <div id="google_ads_iframe_/1090369/C2_0__container__" style="border: 0pt none; width: 468px; height: 60px;"></div></div>
    </div>
    <div id="under_post_kb">
<div class="itnews c_ad_block">
    <b>最新 IT 新闻</b>:
    <br>
 ·              <a href="https://news.cnblogs.com/n/661946/" target="_blank">董明珠京东直播首秀：同框王自如 爆款空调五折</a>
            <br>
 ·              <a href="https://news.cnblogs.com/n/661945/" target="_blank">买早的又成韭菜？特斯拉官宣本月第二次降价 网友神预言</a>
            <br>
 ·              <a href="https://news.cnblogs.com/n/661944/" target="_blank">迅雷会员一个季度猛增60万：收入大涨18.1％</a>
            <br>
 ·              <a href="https://news.cnblogs.com/n/661943/" target="_blank">乐视网股票终止上市：28万股东欲哭无泪 这三点让贾跃亭走到今天</a>
            <br>
 ·              <a href="https://news.cnblogs.com/n/661941/" target="_blank">刘涛今晚直播半价卖房：通宵加班到上班 宵夜变早餐</a>
            <br>
    » <a href="https://news.cnblogs.com/" title="IT 新闻" target="_blank">更多新闻...</a>
</div></div>
    <div id="HistoryToday" class="c_ad_block"></div>
    <script type="text/javascript">
        fixPostBody();
        deliverBigBanner();
setTimeout(function() { incrementViewCount(cb_entryId); }, 50);        deliverAdT2();
        deliverAdC1();
        deliverAdC2();
        loadNewsAndKb();
        loadBlogSignature();
LoadPostCategoriesTags(cb_blogId, cb_entryId);        LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
        GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType);
        loadOptUnderPost();
        GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
    </script>
</div>
	</div><!--end: forFlow -->
	</div><!--end: mainContent 主体内容容器-->

	<div id="sideBar">
		<div id="sideBarMain">
			
<div id="sidebar_news" class="newsItem"><!--done-->
<h3 class="catListTitle">公告</h3>

<div id="blog-news">
    
    <div id="profile_block">
        昵称：
        <a href="https://home.cnblogs.com/u/9dragon/">
            9龙
        </a>
        <br>
        园龄：
        <a href="https://home.cnblogs.com/u/9dragon/" title="入园时间：2019-02-15">
            1年2个月
        </a>
        <br>
        粉丝：
        <a href="https://home.cnblogs.com/u/9dragon/followers/">
            13
        </a>
        <br>
        关注：
        <a href="https://home.cnblogs.com/u/9dragon/followees/">
            4
        </a>
        <div id="p_b_follow">
<a href="javascript:void(0)" onclick="follow(&#39;8a2ececb-93f4-4dbd-b781-08d692b5b804&#39;)">+加关注</a></div>
        <script>getFollowStatus('8a2ececb-93f4-4dbd-b781-08d692b5b804');</script>
    </div>
</div>

</div>

<div id="sidebar_ad"></div>
			<div id="blog-calendar" style="">

<table id="blogCalendar" class="Cal" cellspacing="0" cellpadding="0" title="Calendar" border="0">
    <tbody>
        <tr>
            <td colspan="7">
                <table class="CalTitle" cellspacing="0" border="0">
                    <tbody>
                        <tr>
                            <td class="CalNextPrev">
                                <a href="javascript:void(0);" onclick="loadBlogCalendar(&#39;2020/04/14&#39;); return false;">&lt;</a>
                            </td>
                            <td align="center">2020年5月</td>
                            <td align="right" class="CalNextPrev">
                                <a href="javascript:void(0);" onclick="loadBlogCalendar(&#39;2020/06/14&#39;); return false;">&gt;</a>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
        <tr>
                    <th class="CalDayHeader" align="center" abbr="日" scope="col">日</th>
                    <th class="CalDayHeader" align="center" abbr="一" scope="col">一</th>
                    <th class="CalDayHeader" align="center" abbr="二" scope="col">二</th>
                    <th class="CalDayHeader" align="center" abbr="三" scope="col">三</th>
                    <th class="CalDayHeader" align="center" abbr="四" scope="col">四</th>
                    <th class="CalDayHeader" align="center" abbr="五" scope="col">五</th>
                    <th class="CalDayHeader" align="center" abbr="六" scope="col">六</th>
        </tr>
            <tr>
                            <td class="CalOtherMonthDay" align="center">26</td>
                            <td class="CalOtherMonthDay" align="center">27</td>
                            <td class="CalOtherMonthDay" align="center">28</td>
                            <td class="CalOtherMonthDay" align="center">29</td>
                            <td class="CalOtherMonthDay" align="center">30</td>
                        <td class="" align="center">
                            1
                        </td>
                    <td class="CalWeekendDay" align="center">
                        2
                    </td>
            </tr>
                <tr>
                        <td class="CalWeekendDay" align="center">
                            3
                        </td>
                            <td class="" align="center">
                                4
                            </td>
                            <td class="" align="center">
                                5
                            </td>
                            <td class="" align="center">
                                6
                            </td>
                            <td class="" align="center">
                                7
                            </td>
                            <td class="" align="center">
                                8
                            </td>
                        <td class="CalWeekendDay" align="center">
                            9
                        </td>
                </tr>
                <tr>
                        <td class="CalWeekendDay" align="center">
                            10
                        </td>
                            <td class="" align="center">
                                11
                            </td>
                            <td class="" align="center">
                                12
                            </td>
                            <td class="" align="center">
                                13
                            </td>
                            <td class="CalTodayDay" align="center">
                                14
                            </td>
                            <td class="" align="center">
                                15
                            </td>
                        <td class="CalWeekendDay" align="center">
                            16
                        </td>
                </tr>
                <tr>
                        <td class="CalWeekendDay" align="center">
                            17
                        </td>
                            <td class="" align="center">
                                18
                            </td>
                            <td class="" align="center">
                                19
                            </td>
                            <td class="" align="center">
                                20
                            </td>
                            <td class="" align="center">
                                21
                            </td>
                            <td class="" align="center">
                                22
                            </td>
                        <td class="CalWeekendDay" align="center">
                            23
                        </td>
                </tr>
                <tr>
                        <td class="CalWeekendDay" align="center">
                            24
                        </td>
                            <td class="" align="center">
                                25
                            </td>
                            <td class="" align="center">
                                26
                            </td>
                            <td class="" align="center">
                                27
                            </td>
                            <td class="" align="center">
                                28
                            </td>
                            <td class="" align="center">
                                29
                            </td>
                        <td class="CalWeekendDay" align="center">
                            30
                        </td>
                </tr>
                <tr>
                        <td class="CalWeekendDay" align="center">
                            31
                        </td>
                            <td class="CalOtherMonthDay" align="center">
                                1
                            </td>
                            <td class="CalOtherMonthDay" align="center">
                                2
                            </td>
                            <td class="CalOtherMonthDay" align="center">
                                3
                            </td>
                            <td class="CalOtherMonthDay" align="center">
                                4
                            </td>
                            <td class="CalOtherMonthDay" align="center">
                                5
                            </td>
                        <td class="CalOtherMonthDay" align="center">
                            6
                        </td>
                </tr>
    </tbody>
</table></div><script>loadBlogDefaultCalendar();</script>
			
			<div id="leftcontentcontainer">
				<div id="blog-sidecolumn">
<!-- 搜索 -->
<div id="sidebar_search" class="sidebar-block">
    <div id="sidebar_search" class="mySearch">
        <h3 class="catListTitle">搜索</h3>
        <div id="sidebar_search_box">
            <div id="widget_my_zzk" class="div_my_zzk">
                <input type="text" id="q" onkeydown="return zzk_go_enter(event);" class="input_my_zzk">&nbsp;<input onclick="zzk_go()" type="button" value="找找看" id="btnZzk" class="btn_my_zzk">
            </div>
            <div id="widget_my_google" class="div_my_zzk">
                <input type="text" name="google_q" id="google_q" onkeydown="return google_go_enter(event);" class="input_my_zzk">&nbsp;<input onclick="google_go()" type="button" value="谷歌搜索" class="btn_my_zzk">
            </div>
        </div>
    </div>
</div>

<!-- 常用链接 -->
<div id="sidebar_shortcut" class="sidebar-block">
    <div class="catListLink">
<h3 class="catListTitle">
常用链接
</h3>
<ul>
		<li>

<a href="https://www.cnblogs.com/9dragon/p/" title="我的博客的随笔列表">我的随笔</a>
</li>
		<li>

<a href="https://www.cnblogs.com/9dragon/MyComments.html" title="我的发表过的评论列表">我的评论</a>
</li>
		<li>

<a href="https://www.cnblogs.com/9dragon/OtherPosts.html" title="我评论过的随笔列表">我的参与</a>
</li>
		<li>

<a href="https://www.cnblogs.com/9dragon/RecentComments.html" title="我的博客的评论列表">最新评论</a>
</li>
		<li>

<a href="https://www.cnblogs.com/9dragon/tag/" title="我的博客的标签列表">我的标签</a>
</li>

</ul>
<div id="itemListLin_con" style="display:none;">
<ul>

</ul>
</div>
</div>


</div>

<!-- 最新随笔 -->



<!-- 我的标签 -->
<div id="sidebar_toptags" class="sidebar-block">
    <div class="catListTag">
<h3 class="catListTitle">我的标签</h3>
<ul>

        <li>
            <a href="https://www.cnblogs.com/9dragon/tag/%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">算法与数据结构</a>(8)
        </li>
        <li>
            <a href="https://www.cnblogs.com/9dragon/tag/java%E5%9F%BA%E7%A1%80/">java基础</a>(5)
        </li>
        <li>
            <a href="https://www.cnblogs.com/9dragon/tag/java%E5%9F%BA%E7%A1%80%E3%80%81IO%E6%B5%81/">java基础、IO流</a>(1)
        </li>
        <li>
            <a href="https://www.cnblogs.com/9dragon/tag/JVM/">JVM</a>(1)
        </li>
        <li>
            <a href="https://www.cnblogs.com/9dragon/tag/%E5%8C%BA%E5%9D%97%E9%93%BE/">区块链</a>(1)
        </li>
        <li>
            <a href="https://www.cnblogs.com/9dragon/tag/%E5%BE%92%E6%AD%A5%E3%80%81%E6%84%9F%E6%83%B3/">徒步、感想</a>(1)
        </li>
        <li>
            <a href="https://www.cnblogs.com/9dragon/tag/%E5%BA%8F%E5%88%97%E5%8C%96/">序列化</a>(1)
        </li>
        <li>
            <a href="https://www.cnblogs.com/9dragon/tag/java8/">java8</a>(1)
        </li>
    

</ul>
</div>


</div>

<!-- 积分与排名 -->


<!-- 随笔分类、随笔档案、文章分类、新闻分类、相册、链接 -->
<div id="sidebar_categories">
    
        <div id="sidebar_postcategory" class="catListPostCategory sidebar-block">
            <h3 class="catListTitle">
                

随笔分类



            </h3>


            <ul>

                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/category/1611430.html" rel="" target="">
    java并发(1)
</a>
 

                        </li>
                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/category/1439776.html" rel="" target="">
    Java基础(7)
</a>
 

                        </li>
                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/category/1480402.html" rel="" target="">
    JVM(2)
</a>
 

                        </li>
                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/category/1694822.html" rel="" target="">
    区块链(1)
</a>
 

                        </li>
                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/category/1441733.html" rel="" target="">
    数据结构与算法(8)
</a>
 

                        </li>
                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/category/1470673.html" rel="" target="">
    徒步(1)
</a>
 

                        </li>

            </ul>


        </div>
        <div id="sidebar_postarchive" class="catListPostArchive sidebar-block">
            <h3 class="catListTitle">
                

随笔档案



            </h3>


            <ul>

                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/archive/2020/04.html" rel="" target="">
    2020年4月(1)
</a>
 

                        </li>
                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/archive/2019/12.html" rel="" target="">
    2019年12月(3)
</a>
 

                        </li>
                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/archive/2019/06.html" rel="" target="">
    2019年6月(2)
</a>
 

                        </li>
                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/archive/2019/05.html" rel="" target="">
    2019年5月(5)
</a>
 

                        </li>
                        <li>
                            
<a href="https://www.cnblogs.com/9dragon/archive/2019/04.html" rel="" target="">
    2019年4月(9)
</a>
 

                        </li>

            </ul>


        </div>

</div>

<!-- 最新评论 -->
<div id="sidebar_recentcomments" class="sidebar-block">
    <div class="catListComment">
<h3 class="catListTitle">最新评论</h3>

	<div class="RecentCommentBlock">
        <ul>
                    <li class="recent_comment_title"><a href="https://www.cnblogs.com/9dragon/p/12023971.html">1. Re:CAS都不了解，你还怎么看J.U.C</a></li>
                    <li class="recent_comment_body">楼主文章写得浅显易懂，借楼主宝地寻觅阿里人：阿里菜鸟国际物流技术部大量招人啦，我们主要是为速卖通、淘宝海外、天猫国际、支付宝海淘等出口进口业务提供跨境物流解决方案，目前团队发展迅速，机会多多，HC不限...</li>
                    <li class="recent_comment_author">--庄君祥</li>
                    <li class="recent_comment_title"><a href="https://www.cnblogs.com/9dragon/p/10901448.html">2. Re:java序列化，看这篇就够了</a></li>
                    <li class="recent_comment_body"><p>别的不说，这排版是真的舒服</p>
</li>
                    <li class="recent_comment_author">--流氓呆呆</li>
                    <li class="recent_comment_title"><a href="https://www.cnblogs.com/9dragon/p/10901448.html">3. Re:java序列化，看这篇就够了</a></li>
                    <li class="recent_comment_body"><p>写得很清晰，特意登陆点个赞！</p>
</li>
                    <li class="recent_comment_author">--手机疯</li>
                    <li class="recent_comment_title"><a href="https://www.cnblogs.com/9dragon/p/10901448.html">4. Re:java序列化，看这篇就够了</a></li>
                    <li class="recent_comment_body">写得很透彻</li>
                    <li class="recent_comment_author">--头很痛</li>
                    <li class="recent_comment_title"><a href="https://www.cnblogs.com/9dragon/p/10922205.html">5. Re:徒步四姑娘山二峰有感（5276米）</a></li>
                    <li class="recent_comment_body">@ wainiwann是手机拍的。被移除首页了[摊手]...</li>
                    <li class="recent_comment_author">--9龙</li>
        </ul>
    </div>
</div>


</div>



<!-- 阅读排行榜 -->
<div id="sidebar_topviewedposts" class="sidebar-block">
    <div class="catListView">
<h3 class="catListTitle">阅读排行榜</h3>
	<div id="TopViewPostsBlock">
        <ul style="word-break:break-all">
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10901448.html">
                            1. java序列化，看这篇就够了(18631)
                        </a>
                    </li>
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10710735.html">
                            2. 图解选择排序与插入排序(3552)
                        </a>
                    </li>
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10811316.html">
                            3. 快速排序及其优化(1562)
                        </a>
                    </li>
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10739121.html">
                            4. 最详细版图解优先队列（堆）(1242)
                        </a>
                    </li>
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10720032.html">
                            5. 你知道希尔排序为什么可以打破二次时间界吗？(839)
                        </a>
                    </li>
        </ul>
    </div>
</div>


</div>

<!-- 评论排行榜 -->
<div id="sidebar_topcommentedposts" class="sidebar-block">
    <div class="catListFeedback">
<h3 class="catListTitle">评论排行榜</h3>
	<div id="TopFeedbackPostsBlock">
        <ul style="word-break:break-all">
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10839472.html">
                            1. 告别复杂的流关闭(5)
                        </a>
                    </li>
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10901448.html">
                            2. java序列化，看这篇就够了(5)
                        </a>
                    </li>
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10811316.html">
                            3. 快速排序及其优化(4)
                        </a>
                    </li>
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10720032.html">
                            4. 你知道希尔排序为什么可以打破二次时间界吗？(3)
                        </a>
                    </li>
                    <li>
                        <a href="https://www.cnblogs.com/9dragon/p/10922205.html">
                            5. 徒步四姑娘山二峰有感（5276米）(2)
                        </a>
                    </li>
        </ul>
    </div>
</div>


</div>

<!-- 推荐排行榜 -->
<div id="sidebar_topdiggedposts" class="sidebar-block">
    
<div id="topdigg_posts_wrap">
    <div class="catListView">
        <h3 class="catListTitle">推荐排行榜</h3>
        <div id="TopDiggPostsBlock">
            <ul style="word-break: break-all">
                        <li>
                            <a href="https://www.cnblogs.com/9dragon/p/10901448.html">
                                1. java序列化，看这篇就够了(15)
                            </a>
                        </li>
                        <li>
                            <a href="https://www.cnblogs.com/9dragon/p/12023971.html">
                                2. CAS都不了解，你还怎么看J.U.C(4)
                            </a>
                        </li>
                        <li>
                            <a href="https://www.cnblogs.com/9dragon/p/10913289.html">
                                3. 感受lambda之美，推荐收藏，需要时查阅(3)
                            </a>
                        </li>
                        <li>
                            <a href="https://www.cnblogs.com/9dragon/p/11005996.html">
                                4. 类加载流程，类加载机制及自定义类加载器详解（面试再也不怕了）(3)
                            </a>
                        </li>
                        <li>
                            <a href="https://www.cnblogs.com/9dragon/p/11037283.html">
                                5. 你知道Object中有哪些方法及其作用吗？(3)
                            </a>
                        </li>
            </ul>
        </div>
    </div>
</div>
</div></div>
                    <script>loadBlogSideColumn();</script>
			</div>
			
		</div><!--end: sideBarMain -->
	</div><!--end: sideBar 侧边栏容器 -->
	<div class="clear"></div>
	</div><!--end: main -->
	<div class="clear"></div>
	<div id="footer">
		<!--done-->
Copyright © 2020 9龙
<br><span id="poweredby">Powered by .NET Core on Kubernetes</span>



	</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->


    

</body></html>